/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.service.appointment;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.unis.common.util.TemplateUtil;
import com.unis.dao.IDataSet4iBatis;
import com.unis.model.BaseModel;
import com.unis.model.appointment.Appointment;

/**
 * @author Administrator (email:357375084@qq.com)
 * @since 2013-02-27
 */
@Service("appointmentService")
public class AppointmentServiceImpl implements AppointmentService {

    private IDataSet4iBatis iDataSet4iBatis;

    /****************************************
     * Insert a instace of Appointment
     ****************************************/
    public void insert(Appointment appointment) throws Exception {
        appointment.setAppointid(TemplateUtil.genUUID());
        iDataSet4iBatis.insert(appointment);
    }

    /****************************************
     * Update a instace of Appointment
     ****************************************/
    public void update(Appointment appointment) throws Exception {
        iDataSet4iBatis.update(appointment);
    }

    /****************************************
     * Delete a instace of Appointment
     ****************************************/
    public void delete(Appointment appointment) throws Exception {
        iDataSet4iBatis.delete(appointment);
    }

    /****************************************
     * Insert instaces of SysUser
     ****************************************/
    public void batchInsert(Appointment appointment) throws Exception {
        throw new RuntimeException("Not yet implement!");
    }

    /****************************************
     * Delete instaces of SysUser
     ****************************************/
    public void batchDelete(Appointment appointment) throws Exception {
        String statementId = appointment.getStatmentId();
        String mainId = appointment.getId();
        String[] ids = mainId.split(",");
        List<BaseModel> appointmentList = new ArrayList<BaseModel>();
        for (String id : ids) {
            Appointment model = new Appointment();
            model.setStatmentId(statementId);
            model.setId(id.trim());
            appointmentList.add(model);
        }
        iDataSet4iBatis.batchDelete(appointmentList);
    }

    /****************************************
     * Query instaces of Appointment
     ****************************************/
    public List<BaseModel> queryAsList(Appointment appointment) throws Exception {
        String queryListStatement = "";
        if (appointment.isPaging()) {
            queryListStatement = appointment.getStatmentId();
            appointment.setStatmentId("Appointment.queryRecordsCount");
            BaseModel baseModel = iDataSet4iBatis.queryAsObject(appointment);
            appointment.setTotalCount(baseModel.getTotalCount());
            appointment.setStatmentId(queryListStatement);
        }
        return iDataSet4iBatis.queryAsList(appointment);
    }

    /****************************************
     * Query a instaces of Appointment
     ****************************************/
    public BaseModel queryAsObject(Appointment appointment) throws Exception {
        return iDataSet4iBatis.queryAsObject(appointment);
    }

    @Resource
    public void setiDataSet4iBatis(IDataSet4iBatis iDataSet4iBatis) {
        this.iDataSet4iBatis = iDataSet4iBatis;
    }
}
